TAREA 6 - Grupo 5 (Japan)¶

Integrantes:

  • Palacios Ninahuanca, Ninoska
  • Cerda Rioja, Lorena
  • Lliuya Saldaña, Brayan
In [ ]:
#instalación fiona
!pip install fiona
Collecting fiona
  Downloading fiona-1.10.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (56 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 56.6/56.6 kB 2.7 MB/s eta 0:00:00
Requirement already satisfied: attrs>=19.2.0 in /usr/local/lib/python3.10/dist-packages (from fiona) (24.2.0)
Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from fiona) (2024.8.30)
Requirement already satisfied: click~=8.0 in /usr/local/lib/python3.10/dist-packages (from fiona) (8.1.7)
Collecting click-plugins>=1.0 (from fiona)
  Downloading click_plugins-1.1.1-py2.py3-none-any.whl.metadata (6.4 kB)
Collecting cligj>=0.5 (from fiona)
  Downloading cligj-0.7.2-py3-none-any.whl.metadata (5.0 kB)
Downloading fiona-1.10.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.3 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 17.3/17.3 MB 50.0 MB/s eta 0:00:00
Downloading click_plugins-1.1.1-py2.py3-none-any.whl (7.5 kB)
Downloading cligj-0.7.2-py3-none-any.whl (7.1 kB)
Installing collected packages: cligj, click-plugins, fiona
Successfully installed click-plugins-1.1.1 cligj-0.7.2 fiona-1.10.1
In [ ]:
# SE IMPORTAN LOS ARCHIVOS GPKG PASADOS DE JAPON (tarea 4)
import geopandas as gpd

Japon_Adm = gpd.read_file("https://github.com/brayan-lliuya-2024/TAREA_4-Grupo5/raw/refs/heads/main/maps/JaponAdm.gpkg")
japon_states = gpd.read_file("https://github.com/brayan-lliuya-2024/TAREA_4-Grupo5/raw/refs/heads/main/maps/JaponAdm.gpkg", layer="Estados")
japon_municipalities = gpd.read_file("https://github.com/brayan-lliuya-2024/TAREA_4-Grupo5/raw/refs/heads/main/maps/JaponAdm.gpkg", layer="Municipios")
/usr/local/lib/python3.10/dist-packages/pyogrio/geopandas.py:265: UserWarning: More than one layer found in 'JaponAdm.gpkg': 'Estados' (default), 'Municipios'. Specify layer parameter to avoid this warning.
  result = read_func(
In [ ]:
# SE IMPORTA LA CAPA DE RIOS (tarea 4)
rivers = gpd.read_file("https://github.com/brayan-lliuya-2024/TAREA_4-Grupo5/raw/refs/heads/main/maps/MapasJapon.gpkg", layer="rivers_jp")
In [ ]:
# SE IMPORTA LOS AEROPUERTOS DE JAPON
airports=gpd.read_file("https://github.com/brayan-lliuya-2024/TAREA_4-Grupo5/raw/refs/heads/main/maps/mapsjapan_ej2.gpkg",layer="airports")
In [ ]:
# SE IMPORTA EL BORDE DEL PAÍS
border=gpd.read_file("https://github.com/brayan-lliuya-2024/TAREA_4-Grupo5/raw/refs/heads/main/maps/mapsjapan_ej3.gpkg")
In [ ]:
# IMPORTANDO DATA DE PUERTOS A NIVEL MUNDIAL
import pandas as pd

portsFileLink="https://github.com/CienciaDeDatosEspacial/GeoDataFrame_Analytics/raw/main/data/UpdatedPub150.csv"
infoseaports=pd.read_csv(portsFileLink)

infoseaports.head(3)
Out[ ]:
World Port Index Number Region Name Main Port Name Alternate Port Name UN/LOCODE Country Code World Water Body IHO S-130 Sea Area Sailing Direction or Publication Publication Link ... Supplies - Fuel Oil Supplies - Diesel Oil Supplies - Aviation Fuel Supplies - Deck Supplies - Engine Repairs Dry Dock Railway Latitude Longitude
0 7950.0 United States E Coast -- 6585 Maurer United States North Atlantic Ocean U.S. Coast Pilot 2 - Atlantic Coast: Cape Cod ... https://nauticalcharts.noaa.gov/publications/c... ... Yes Yes Unknown Yes Yes Moderate Unknown Unknown 40.533333 -74.250000
1 52235.0 Sulawesi -- 51970 Mangkasa Oil Terminal Indonesia Teluk Bone; Banda Sea; South Pacific Ocean Sailing Directions Pub. 163 (Enroute) - Borneo... https://msi.geo.nga.mil/api/publications/downl... ... No No Unknown No No NaN Unknown Unknown -2.733333 121.066667
2 47620.0 Madagascar -- 47350 Iharana Madagascar Indian Ocean Sailing Directions Pub. 171 (Enroute) - East A... https://msi.geo.nga.mil/api/publications/downl... ... No No Unknown No No Emergency Only Unknown Unknown -13.350000 50.000000

3 rows × 108 columns

In [ ]:
#  SELECCIONAR SOLO COLUMNAS DE INTERÉS
infoseaports.rename(columns={'Main Port Name':'portName'},inplace=True)
infoseaports=infoseaports.loc[:,['portName', 'Country Code','Latitude', 'Longitude']]

infoseaports.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3739 entries, 0 to 3738
Data columns (total 4 columns):
 #   Column        Non-Null Count  Dtype  
---  ------        --------------  -----  
 0   portName      3739 non-null   object 
 1   Country Code  3739 non-null   object 
 2   Latitude      3739 non-null   float64
 3   Longitude     3739 non-null   float64
dtypes: float64(2), object(2)
memory usage: 117.0+ KB
In [ ]:
# PROYECTANDO LA DATA
seaports=gpd.GeoDataFrame(data=infoseaports.copy(),
                           geometry=gpd.points_from_xy(infoseaports.Longitude,
                                                       infoseaports.Latitude),
                          crs=6684)

# separar solo para Japón
seaports_jap=seaports[seaports['Country Code']=='Japan'].copy()

# Reasignando índices
seaports_jap.reset_index(drop=True, inplace=True)

# reprojectando
seaports_jap_6684=seaports_jap.to_crs(6684)

seaports_jap_6684.plot(markersize=3)
Out[ ]:
<Axes: >
No description has been provided for this image
In [ ]:
# Seleccionando solo aeropuestos grandes
largeAirports=airports[airports['kind']=='large_airport']
largeAirports.reset_index(drop=True, inplace=True)

# Ploteo
base=largeAirports.plot(color='red',marker="^")
seaports_jap_6684.plot(ax=base,alpha=0.5, markersize=3)
Out[ ]:
<Axes: >
No description has been provided for this image

Extracción de Ubicaciones Espaciales¶

Distancia entre puntos¶

In [ ]:
seaports_jap_6684.head()
Out[ ]:
portName Country Code Latitude Longitude geometry
0 Mombetsu Ko Japan 44.350000 143.350000 POINT (143.35 44.35)
1 Iwakuni Ko Japan 34.183333 132.250000 POINT (132.25 34.183)
2 Hitachi Japan 36.500000 140.633333 POINT (140.633 36.5)
3 Kushiro Ko Japan 42.983333 144.366667 POINT (144.367 42.983)
4 Hanasaki Ko Japan 43.283333 145.583333 POINT (145.583 43.283)
In [ ]:
largeAirports.head()
Out[ ]:
name kind latitude_deg longitude_deg elevation_ft iso_region municipality geometry
0 Narita International Airport large_airport 35.764702 140.386002 141.0 JP-12 Narita POINT (1487953.425 1208863.893)
1 Kansai International Airport large_airport 34.427299 135.244003 26.0 JP-27 Osaka POINT (1035834.881 992037.816)
2 New Chitose Airport large_airport 42.775200 141.692001 82.0 JP-01 Sapporo POINT (1449235.049 2015248.441)
3 Fukuoka Airport large_airport 33.585899 130.451004 32.0 JP-40 Fukuoka POINT (599284.927 859521.885)
4 Kagoshima Airport large_airport 31.803400 130.718994 906.0 JP-46 Kagoshima POINT (636838.567 662888.116)
In [ ]:
# distancia entre 'Mombetsu Ko' y 'Narita International Airportl' en km
largeAirports.iloc[0].geometry.distance(seaports_jap_6684.iloc[0].geometry)/1000
Out[ ]:
1916.982917535566
In [ ]:
# Distancias entre aeropuertos y puertos
seaports_jap_6684.geometry.apply\
(lambda g: largeAirports.geometry.distance(g)/1000)
Out[ ]:
0 1 2 3 4 5 6 7 8 9 10 11
0 1916.982918 1434.122723 2482.118909 1047.698556 919.098829 1580.202859 1473.005814 2101.240880 1856.072084 1832.986905 365.375780 378.854480
1 1916.997943 1434.137771 2482.133644 1047.713243 919.113851 1580.217911 1473.020856 2101.255919 1856.087113 1833.001949 365.387595 378.866695
2 1916.989976 1434.130115 2482.126869 1047.706549 919.106372 1580.210132 1473.013259 2101.248347 1856.079158 1832.994072 365.379065 378.858098
3 1916.982990 1434.122934 2482.119425 1047.699095 919.099111 1580.203018 1473.006049 2101.241126 1856.072163 1832.987021 365.374866 378.853630
4 1916.981857 1434.121848 2482.118472 1047.698153 919.098051 1580.201914 1473.004972 2101.240052 1856.071031 1832.985901 365.373631 378.852387
... ... ... ... ... ... ... ... ... ... ... ... ...
158 1916.992567 1434.132681 2482.129318 1047.708986 919.108920 1580.212709 1473.015820 2101.250905 1856.081748 1832.996657 365.381301 378.860391
159 1916.999123 1434.138895 2482.134616 1047.714202 919.114943 1580.219056 1473.021969 2101.257028 1856.088290 1833.003112 365.388927 378.868032
160 1916.994056 1434.134000 2482.130225 1047.709858 919.110149 1580.214094 1473.017108 2101.252181 1856.083230 1832.998095 365.383681 378.862733
161 1916.991494 1434.131654 2482.128421 1047.708100 919.107920 1580.211664 1473.014802 2101.249891 1856.080677 1832.995597 365.380118 378.859201
162 1916.984800 1434.124572 2482.120640 1047.700274 919.100657 1580.204722 1473.007656 2101.242719 1856.073965 1832.988779 365.377511 378.856244

163 rows × 12 columns

In [ ]:
# Asignar los nombres de aeropuertos y puertos como índices

seaports_jap_6684.set_index('portName').geometry.apply\
(lambda g: largeAirports.set_index('name').geometry.distance(g)/1000)
Out[ ]:
name Narita International Airport Kansai International Airport New Chitose Airport Fukuoka Airport Kagoshima Airport Chubu Centrair International Airport Osaka International Airport Sendai Airport Tokyo Haneda International Airport Yokota Air Base Naha Airport / JASDF Naha Air Base Kadena Air Base
portName
Mombetsu Ko 1916.982918 1434.122723 2482.118909 1047.698556 919.098829 1580.202859 1473.005814 2101.240880 1856.072084 1832.986905 365.375780 378.854480
Iwakuni Ko 1916.997943 1434.137771 2482.133644 1047.713243 919.113851 1580.217911 1473.020856 2101.255919 1856.087113 1833.001949 365.387595 378.866695
Hitachi 1916.989976 1434.130115 2482.126869 1047.706549 919.106372 1580.210132 1473.013259 2101.248347 1856.079158 1832.994072 365.379065 378.858098
Kushiro Ko 1916.982990 1434.122934 2482.119425 1047.699095 919.099111 1580.203018 1473.006049 2101.241126 1856.072163 1832.987021 365.374866 378.853630
Hanasaki Ko 1916.981857 1434.121848 2482.118472 1047.698153 919.098051 1580.201914 1473.004972 2101.240052 1856.071031 1832.985901 365.373631 378.852387
... ... ... ... ... ... ... ... ... ... ... ... ...
Shimizu Ko 1916.992567 1434.132681 2482.129318 1047.708986 919.108920 1580.212709 1473.015820 2101.250905 1856.081748 1832.996657 365.381301 378.860391
Shimonoseki 1916.999123 1434.138895 2482.134616 1047.714202 919.114943 1580.219056 1473.021969 2101.257028 1856.088290 1833.003112 365.388927 378.868032
Tsuruga Ko 1916.994056 1434.134000 2482.130225 1047.709858 919.110149 1580.214094 1473.017108 2101.252181 1856.083230 1832.998095 365.383681 378.862733
Yokosuka Ko 1916.991494 1434.131654 2482.128421 1047.708100 919.107920 1580.211664 1473.014802 2101.249891 1856.080677 1832.995597 365.380118 378.859201
Wakkanai 1916.984800 1434.124572 2482.120640 1047.700274 919.100657 1580.204722 1473.007656 2101.242719 1856.073965 1832.988779 365.377511 378.856244

163 rows × 12 columns

In [ ]:
# Ordenar alfabeticamente filas y columnas
seaports_jap_6684.set_index('portName').geometry.apply\
(lambda g: largeAirports.set_index('name').geometry.distance(g)/1000).\
sort_index(axis=0).sort_index(axis=1)
Out[ ]:
name Chubu Centrair International Airport Fukuoka Airport Kadena Air Base Kagoshima Airport Kansai International Airport Naha Airport / JASDF Naha Air Base Narita International Airport New Chitose Airport Osaka International Airport Sendai Airport Tokyo Haneda International Airport Yokota Air Base
portName
Abashiri Ko 1580.202389 1047.698295 378.853592 919.098423 1434.122279 365.374874 1916.982403 2482.118635 1473.005383 2101.240455 1856.071573 1832.986413
Aioi 1580.215862 1047.711484 378.864424 919.111882 1434.135755 365.385341 1916.995845 2482.131863 1473.018856 2101.253926 1856.085017 1832.999874
Akita-Funakawa Ko 1580.208395 1047.704211 378.858327 919.104434 1434.128291 365.379441 1916.988387 2482.124566 1473.011396 2101.246468 1856.077558 1832.992409
Amagasaki 1580.215237 1047.711028 378.863523 919.111307 1434.135151 365.384433 1916.995186 2482.131395 1473.018262 2101.253336 1856.084361 1832.999232
Aokata 1580.221079 1047.716048 378.869997 919.116921 1434.140900 365.390858 1917.001171 2482.136477 1473.023966 2101.259020 1856.090336 1833.005148
... ... ... ... ... ... ... ... ... ... ... ... ...
Yawatahama 1580.218278 1047.713750 378.866616 919.114264 1434.138158 365.387483 1916.998276 2482.134142 1473.021252 2101.256319 1856.087448 1833.002299
Yokkaichi 1580.214130 1047.710094 378.862250 919.110249 1434.134064 365.383166 1916.994047 2482.130449 1473.017184 2101.252262 1856.083225 1832.998108
Yokohama Ko 1580.211615 1047.708011 378.859264 919.107857 1434.131599 365.380189 1916.991453 2482.128334 1473.014744 2101.249832 1856.080636 1832.995552
Yokosuka Ko 1580.211664 1047.708100 378.859201 919.107920 1434.131654 365.380118 1916.991494 2482.128421 1473.014802 2101.249891 1856.080677 1832.995597
Yura 1580.215950 1047.711805 378.863892 919.112045 1434.135874 365.384770 1916.995879 2482.132169 1473.018990 2101.254066 1856.085055 1832.999935

163 rows × 12 columns

In [ ]:
distanceMatrixKM_sea_air= seaports_jap_6684.set_index('portName').geometry.apply\
                          (lambda g: largeAirports.set_index('name').geometry.distance(g)/1000).\
                          sort_index(axis=0).sort_index(axis=1)
In [ ]:
# La distancia media desde un puerto marítimo hasta todos los grandes aeropuertos
distanceMatrixKM_sea_air.mean(axis=1).sort_values(ascending=True)
Out[ ]:
0
portName
Nemuro Ko 1448.978992
Hanasaki Ko 1448.979022
Abashiri Ko 1448.979559
Mombetsu Ko 1448.980061
Kushiro Ko 1448.980119
... ...
Nakagusuku 1449.002571
Nishihara 1449.002689
Naha Ko 1449.002777
Hirara Ko 1449.005437
Ishigaki 1449.006559

163 rows × 1 columns


In [ ]:
# distancias promedio, mínimas y máximas de los puertos hacia los aeropuertos
SomeStats=pd.DataFrame()
SomeStats['mean']=distanceMatrixKM_sea_air.mean(axis=1)
SomeStats['min']=distanceMatrixKM_sea_air.min(axis=1)
SomeStats['max']=distanceMatrixKM_sea_air.max(axis=1)

SomeStats.head(10)
Out[ ]:
mean min max
portName
Abashiri Ko 1448.979559 365.374874 2482.118635
Aioi 1448.992511 365.385341 2482.131863
Akita-Funakawa Ko 1448.985324 365.379441 2482.124566
Amagasaki 1448.991871 365.384433 2482.131395
Aokata 1448.997660 365.390858 2482.136477
Aomori Ko 1448.984137 365.378631 2482.123282
Atsumi 1448.990624 365.382756 2482.130439
Beppu 1448.995615 365.388390 2482.134776
Chiba Ko 1448.987813 365.379697 2482.127944
Choshi 1448.987133 365.378905 2482.127365
In [ ]:
# aeropuerto más alejado de cada puerto marítimo
distanceMatrixKM_sea_air.idxmax(axis=1)
Out[ ]:
0
portName
Abashiri Ko New Chitose Airport
Aioi New Chitose Airport
Akita-Funakawa Ko New Chitose Airport
Amagasaki New Chitose Airport
Aokata New Chitose Airport
... ...
Yawatahama New Chitose Airport
Yokkaichi New Chitose Airport
Yokohama Ko New Chitose Airport
Yokosuka Ko New Chitose Airport
Yura New Chitose Airport

163 rows × 1 columns


In [ ]:
# puerto marítimo más alejado de cada aeropuerto
distanceMatrixKM_sea_air.idxmax(axis=0)
Out[ ]:
0
name
Chubu Centrair International Airport Ishigaki
Fukuoka Airport Ishigaki
Kadena Air Base Ishigaki
Kagoshima Airport Ishigaki
Kansai International Airport Ishigaki
Naha Airport / JASDF Naha Air Base Ishigaki
Narita International Airport Ishigaki
New Chitose Airport Ishigaki
Osaka International Airport Ishigaki
Sendai Airport Ishigaki
Tokyo Haneda International Airport Ishigaki
Yokota Air Base Ishigaki

In [ ]:
# aeropuerto más cercano a cada puerto marítimo
distanceMatrixKM_sea_air.idxmin(axis=1)
Out[ ]:
0
portName
Abashiri Ko Naha Airport / JASDF Naha Air Base
Aioi Naha Airport / JASDF Naha Air Base
Akita-Funakawa Ko Naha Airport / JASDF Naha Air Base
Amagasaki Naha Airport / JASDF Naha Air Base
Aokata Naha Airport / JASDF Naha Air Base
... ...
Yawatahama Naha Airport / JASDF Naha Air Base
Yokkaichi Naha Airport / JASDF Naha Air Base
Yokohama Ko Naha Airport / JASDF Naha Air Base
Yokosuka Ko Naha Airport / JASDF Naha Air Base
Yura Naha Airport / JASDF Naha Air Base

163 rows × 1 columns


In [ ]:
# puerto marítimo más cercano a cada aeropuerto
distanceMatrixKM_sea_air.idxmin(axis=0)
Out[ ]:
0
name
Chubu Centrair International Airport Nemuro Ko
Fukuoka Airport Nemuro Ko
Kadena Air Base Nemuro Ko
Kagoshima Airport Nemuro Ko
Kansai International Airport Nemuro Ko
Naha Airport / JASDF Naha Air Base Nemuro Ko
Narita International Airport Nemuro Ko
New Chitose Airport Nemuro Ko
Osaka International Airport Nemuro Ko
Sendai Airport Nemuro Ko
Tokyo Haneda International Airport Nemuro Ko
Yokota Air Base Nemuro Ko

Ejercicio 2¶

  1. Utilice un mapa de puntos y un mapa de líneas de su país.
  2. Calcule la matriz de distancias para ambos.
  3. Seleccione una línea de la matriz de distancias y trace el punto más cercano y el más lejano a esa línea.
In [ ]:
rivers.head()
Out[ ]:
f_code hyc lit nam soc geometry
0 BH140 8 1 UNK JPN LINESTRING (373410.491 44638.656, 373749.791 4...
1 BH140 8 1 UNK JPN LINESTRING (373749.791 44838.884, 373376.037 4...
2 BH140 8 1 UNK JPN LINESTRING (379093.829 45402.433, 379850.108 4...
3 BH140 8 1 UNK JPN LINESTRING (373749.791 44838.884, 375023.404 4...
4 BH140 8 1 UNK JPN LINESTRING (375437.64 45633.307, 375217.713 45...
In [ ]:
rivers
Out[ ]:
f_code hyc lit nam soc geometry
0 BH140 8 1 UNK JPN LINESTRING (373410.491 44638.656, 373749.791 4...
1 BH140 8 1 UNK JPN LINESTRING (373749.791 44838.884, 373376.037 4...
2 BH140 8 1 UNK JPN LINESTRING (379093.829 45402.433, 379850.108 4...
3 BH140 8 1 UNK JPN LINESTRING (373749.791 44838.884, 375023.404 4...
4 BH140 8 1 UNK JPN LINESTRING (375437.64 45633.307, 375217.713 45...
... ... ... ... ... ... ...
4790 BH140 8 2 ONUMA JPN LINESTRING (1391412.015 2307235.768, 1391198.8...
4791 BH140 8 1 KOETOI G. JPN LINESTRING (1390505.97 2308978.942, 1390526.22...
4792 BH140 8 1 UNK JPN LINESTRING (1396542.534 2313815.963, 1396963.1...
4793 BH140 8 1 UNK JPN LINESTRING (1920917.686 2466259.985, 1921504.2...
4794 BH140 8 2 UNK JPN LINESTRING (1165502.075 1200379.89, 1165902.75...

4795 rows × 6 columns

In [ ]:
print(len(rivers['nam'].unique()))
382
In [ ]:
#Se entiende a los datos que contienen "UNK" como desconocidos
rivers_with_unk = rivers[rivers.nam.str.contains('UNK')]
print(rivers_with_unk['nam'].unique())
['UNK' 'UNK\r\n' 'UNK\r\n\r\n' 'UNK    ISHIKARI GAWA TO TUNAGATTEIRUKAMO']
In [ ]:
#Se filtrarán los ríos desconocidos ("UNK")
rivers = rivers[~rivers.nam.str.contains('UNK')]

#Se cambia "nam" a "name" para un mejor entendimiento
rivers = rivers.rename(columns={'nam': 'name'})
In [ ]:
print(len(rivers['name'].unique()))
378
In [ ]:
rivers.head()
Out[ ]:
f_code hyc lit name soc geometry
18 BH140 8 1 ANBO G. JPN LINESTRING (640341.959 494783.487, 640533.83 4...
23 BH140 8 1 KIMOTSUKI G. JPN LINESTRING (668114.864 615221.236, 666979.228 ...
25 BH140 8 1 KIMOTSUKI G. JPN LINESTRING (663537.594 615098.715, 661949.242 ...
26 BH140 8 1 MANOSE G. JPN LINESTRING (608555.528 613449.112, 608760.762 ...
27 BH140 8 1 MANOSE G. JPN LINESTRING (599901.222 620731.187, 600053.928 ...
In [ ]:
rivers['name'].unique()
Out[ ]:
array(['ANBO G.', 'KIMOTSUKI G.', 'MANOSE G.', 'HIROTO G.', 'HISHIDA G.',
       'SHIN K.', 'SENDAI G.', 'OYODO G.', 'IWASE G.', 'HITOTSUSE G.',
       'OMARU G.', 'KUMA G.', 'MIMI G.', 'KAWABE G.', 'ISUZU G.',
       'MIDORI K.', 'KITA G.', 'SHIRA K.', 'KIKUCHI G.', 'BANJO G.',
       'ONO G.', 'SHIMANTO G.', 'MATSUDA G.', 'YABE G.', 'CHIKUGO G.',
       'OITA G.', 'KASE G.', 'KANOGAWA KO', 'HIJI G.', 'NIIDA G.',
       'MATSUURA G.', 'YAMAGUNI G.', 'YUSUHARA G.', 'YAKKAN G.',
       'HARAI K.', 'KOZA G.', 'NIYODO G.', 'NAHARI G.', 'ONGA G.',
       'KAIFU G.', 'AKI G.', 'HIKI G.', 'MANOBE G.', 'KUMANO G.',
       'YOSHINO G.', 'HYTkakunin', 'SHIGENOBU G.', 'NAKA G.',
       'NAKAYAMA G.', 'DOZUN G.', 'KAMO G.', 'TONDA G.', 'HOUOU KO',
       'IYA G.', 'KOTO G.', 'HIDAKA G.', 'KITAYAMA G.', 'ASA G.',
       'KATSUURA G.', 'YOSHIDA G.', 'FUSHINO G.', 'TOTSU K.', 'ARIDA G.',
       'SHIMATA G.', 'AKUI G.', 'ONO KO', 'SAITA G.', 'NISHIKI G.',
       'SABA G.', 'KINO G.', 'OZE G.', 'DOKI G.', 'ABU G.', 'KUROSE G.',
       'TAKATSU G.', 'AHIDA G.', 'NUTO G.', 'MIYA G.', 'ASHIDA G.',
       'YAMATO G.', 'HIKIMI G.', 'ODA G.', 'ASAHI G.', 'KUMODE G.',
       'IZUMO G.', 'KIKU G.', 'KUMOZUFURU K.', 'TENRYU G.', 'ENO K.',
       'OTA G.', 'NABARI G.', 'HAMANA KO', 'YODO G.', 'INA G.', 'OI G.',
       'TAKAHASHI G.', 'IGA G.', 'MISUMI G.', 'IBO G.', 'NARIWA G.',
       'SUFU G.', 'SAIJO G.', 'MUKO G.', 'CHIKUSA G.', 'KIZU G.',
       'UJI G.', 'HOZU G.', 'FUNAGIRA DAMUKO', 'SUZUKA G.', 'YAHAGI G.',
       'ABE K.', 'ICHI K.', 'KONO G.', 'KANNOSE G.', 'GONO G.', 'KETA G.',
       'KISO G.', 'KANDO G.', 'IBI G.', 'YUMESAKI G.', 'ETI G.',
       'NIKKO G.', 'ECHI G.', 'HII K.', 'SHONAI G.', 'OCHISE G.',
       'FUJI K.', 'KANSA G.', 'YASU G.', 'HINO G.', 'MIDORI KO',
       'MACHIYA G.', 'TOKI G.', 'SESSO KO', 'SHIZUMA G.', 'MARUYAMA G.',
       'IKAWA KO', 'YURA G.', 'SAKAWA G.', 'HAYA K.', 'ISUMI G.',
       'KOITO G.', 'TENJIN G.', 'INASHI G.', 'ADO G.', 'FUJI G.\r\n',
       'OBITSU G.', 'HAKUTA G.', 'SAGAMI G.', 'CYUKAI', 'SINJI KO',
       'YATA G.', 'MINAMI G.', 'TSURUMI G.', 'TAMA G.', 'YORO G.',
       'DASHI G.', 'FUEFUKI G.', 'SAKAI G.', 'KATSURA G.', 'KISHIDA G.',
       'NEO G.', 'MURAYAMA G.', 'KAMANASHI G.', 'ASA K.', 'EDO G.',
       'ARA K.', 'NAGARA G.', 'TAKENO G.', 'MASE G.', 'MASHITA G.',
       'TOUSENKYO KANAYAMA KO', 'INBA NUMA', 'OTAKI G.', 'TONE G.',
       'MIBU G.', 'MIURA TYOSUIIKE', 'FURUTONE G.', 'IRUMA G.',
       'KITA URA', 'KINU G,', 'KASUMI-GA URA', 'KOMA G.', 'MOTO ARA K.',
       'SUWA KO', 'ASUWA G.', 'CHIKUMA G.', 'NARAI G.', 'AZUSA KO',
       'KOKAI G.', 'OMOI G,', 'OMOI G.', 'SAKURA G.', 'KUZURYU G.',
       'AZUSA G.', 'WATARASE G.', 'TAISYO IKE', 'KABURA G.', 'KARASU G.',
       'KINU G.', 'TAKAHARA G.', 'HIMUMA', 'DAIYOJI G.', 'HINUMA G.',
       'SHIBAYAMA G.', 'TAKASE G.', 'KAKEHASHI G.', 'SAI G.', 'TEDORI G.',
       'KUJI G.', 'AGATSUMA G.', 'SHO K.', 'JINZU G.', 'JOZANJI G.',
       'SUSOBANA G.', 'JOUGANJI G.', 'KATASHINA G.', 'HIME K.', 'TA GAWA',
       'CHUZENJI KO', 'DAIYA G.', 'HOKI G.', 'KIYOTSU G.', 'SAME K.',
       'NAKATSU G.', 'SABI G.', 'NATSUI G.', 'SHINANO G.', 'SABAISHI G.',
       'HOKURA G.', 'ABUKUMA G.', 'TADAMI G.', 'KIDO G.', 'INAWASHIRO KO',
       'O KAWA', 'SHIBUMI G.', 'UKEDO G.', 'TOKONAMI G.',
       'NAKA NO KUCHI G.', 'AGANO G.', 'HIBARA KO', 'HAYADE G.',
       'MANO G.', 'HAYAMA KO', 'KAJI G.', 'TAINAI G.', 'SHIROSHI G.',
       'SHIROISHI G.', 'MOGAMI G.', 'MATSU K.', 'ABUKUBA G.', 'NATORI G.',
       'MIOMOTE G.', 'NANAKITA G.', 'HIROSE G.', 'SUKAWA G.',
       'KITAKAMI G.', 'EAI G.', 'OGUNI G.', 'HASAMA G.', 'AKA G.',
       'KESEN G.', 'MINASE G.', 'ISAWA G.', 'WAGA G.', 'KOYOSHI G.',
       'SARUKAISHI G.', 'OMONO G.', 'HEI G.', 'OMOTO G.', 'BABAME G.',
       'AKKA G.', 'ANI G.', 'YONESHIRO G.', 'UGE G.', 'MABECHI G.',
       'NIIBA G.', 'OIRASE G.', 'ASASEISHI G.', 'GONOHE G.', 'AISAKA G.',
       'ARA KAWA', 'SHITTSUNOHE G.', 'AKAISHI G.', 'NAKAMURA G.',
       'OGAWARA KO', 'IWAKI G.', 'TONABU G.', 'SHIRIUCHI G.', 'ASSABU G.',
       'HIDAKA HOROBETSU G.', 'YURAPPU G.', 'SHIZUNAI G.', 'MOTOURA K.',
       'TOYONI G.', 'SHIRIBESHI TOSHIBETSU G.', 'REKIFUNE G.',
       'APPETSU G.', 'OSARU G.', 'NIIKAPPU G.', 'SARU G.', 'MUKABIRA G.',
       'SHIRAOI G.', 'TOKACHI G.', 'SHIKOTSU KO', 'YUFUTSU G.', 'MU KAWA',
       'AZUMA G.', 'SATSUNAI G.', 'AZUMA G.\r\n\r\n', 'URAHORO G.',
       'ABIRA G.', 'SHIRIBETSU G.', 'CHITOSE G.', 'ONBETSU G.',
       'HORIKAPPU G.', 'SHIKARIBETSU G.', 'YUBARI G.', 'KUSHIRO G.',
       'TOYOHIRA G.', 'AKAN G.', 'OTOFUKE G.', 'EBETSU G.',
       'TOSHIBETSU G.', 'ISHIKARI G.', 'TOBETSU G.',
       'BARATO G.  ISHIKARI GAWA TO TUNAGATTEIRUKAMO', 'YOTCHI G.',
       'BIRIBETSU G.', 'SAHORO G.', 'CHARO G.', 'ASHORO G.',
       'BEKANBEUSHI G.', 'SHORO G.', 'SETSURI G.', 'FUREN G.', 'AKAN KO',
       'TOKOTAN G.', 'NISHIBETSU G.', 'ASHIBETSU G.', 'SHUNBETSU G.',
       'SORACHI G.', 'TOHORO G.', 'TOKORO G.', 'OKETO KO', 'SHIBETSU G.',
       'CHUBETSU G.', 'KUSSHARO KO', 'CHURUI G.', 'MUKA G.',
       'YANBETSU G.', 'SHARI G.', 'URYU G.', 'TOFUTSU KO', 'MOKOTO G.',
       'RUMAI G.', 'ABASHIRI  KO', 'URYUG.', 'YUBETSU G.',
       'OBIRASHIBE G.', 'SAROMABETSU G.', 'KOTANBETSU G.',
       'SHUMARINAI KO', 'NAYORO G.', 'HABORO G.', 'OKOPPE G.',
       'MOKOPPE G.', 'ENBETSU G.', 'TESHIO G.', 'TOKUSHIBETSU G.',
       'HOROBETSU G.', 'SAROBETSU G.', 'TONBETSU G.', 'RUBETSU G.',
       'KUCCHARO KO', 'SARUFUTSU G.', 'KOETOI G.', 'ONUMA'], dtype=object)
In [ ]:
#Buscamos uno de ellos
rivers[rivers.name.str.contains('TOHORO')]
Out[ ]:
f_code hyc lit name soc geometry
4456 BH140 8 1 TOHORO G. JPN LINESTRING (1712400.349 2176951.417, 1712115.3...
In [ ]:
#Distancia de cada aeropuerto a TOHORO
rivers[rivers.name.str.contains('TOHORO')].iloc[0].geometry.distance(largeAirports.set_index('name').geometry)/1000
Out[ ]:
geometry
name
Narita International Airport 964.139995
Kansai International Airport 1328.822323
New Chitose Airport 270.397139
Fukuoka Airport 1686.891758
Kagoshima Airport 1820.218243
Chubu Centrair International Airport 1207.474836
Osaka International Airport 1285.612683
Sendai Airport 696.895538
Tokyo Haneda International Airport 1007.961540
Yokota Air Base 1004.373970
Naha Airport / JASDF Naha Air Base 2501.407196
Kadena Air Base 2480.169216

In [ ]:
distanceMatrixKM_riv_air=rivers.set_index('name').geometry.apply\
(lambda g: largeAirports.set_index('name').geometry.distance(g)/1000).\
sort_index(axis=0).sort_index(axis=1)
distanceMatrixKM_riv_air
Out[ ]:
name Chubu Centrair International Airport Fukuoka Airport Kadena Air Base Kagoshima Airport Kansai International Airport Naha Airport / JASDF Naha Air Base Narita International Airport New Chitose Airport Osaka International Airport Sendai Airport Tokyo Haneda International Airport Yokota Air Base
name
ABASHIRI KO 1217.283686 1677.182631 2480.229305 1816.532215 1333.154795 2501.435605 990.433074 244.551514 1289.520308 718.752026 1031.244435 1024.571465
ABASHIRI KO 1221.450033 1681.065622 2484.413919 1820.660498 1337.342462 2505.620110 994.375482 248.060198 1293.708457 722.761821 1035.246731 1028.624369
ABASHIRI KO 1221.883647 1681.065622 2484.483807 1820.660498 1337.587105 2505.688622 995.366575 248.060198 1293.941167 723.638463 1036.136728 1029.413953
ABE K. 145.207525 752.649206 1404.981922 802.095667 296.588420 1426.017681 208.081027 934.788240 272.380742 429.855373 146.885518 127.463080
ABE K. 147.570407 755.686219 1416.144168 808.983507 300.281871 1437.214887 197.428037 898.156506 273.393886 398.721079 137.065171 106.327763
... ... ... ... ... ... ... ... ... ... ... ... ...
YURA G. 136.271003 489.826883 1232.644105 580.834985 93.659352 1253.887470 463.826545 1014.210373 52.156038 598.567185 405.740026 367.973183
YURA G. 146.116346 474.084438 1223.352566 568.810180 96.626687 1244.586170 471.604453 1015.365659 57.933202 602.928759 413.801842 375.619762
YURA G. 132.514027 498.133515 1237.317549 586.957292 95.367980 1258.564476 459.421105 1012.425724 53.114370 594.698752 401.353183 363.562105
YURAPPU G. 891.326203 1302.876721 2119.461831 1450.901618 988.902091 2140.558714 738.826818 131.905972 944.783387 472.158064 763.389468 743.754020
YUSUHARA G. 393.011867 232.048127 913.742596 263.398089 240.533547 934.955208 740.857837 1306.427070 275.752309 902.421690 679.818443 649.522678

1273 rows × 12 columns

In [ ]:
#Aquí, vemos una fila (río, nombre exacto), que indica la distancia a cada columna (gran aeropuerto)
distanceMatrixKM_riv_air.loc['TOHORO G.'].sort_values()
Out[ ]:
TOHORO G.
name
New Chitose Airport 270.397139
Sendai Airport 696.895538
Narita International Airport 964.139995
Yokota Air Base 1004.373970
Tokyo Haneda International Airport 1007.961540
Chubu Centrair International Airport 1207.474836
Osaka International Airport 1285.612683
Kansai International Airport 1328.822323
Fukuoka Airport 1686.891758
Kagoshima Airport 1820.218243
Kadena Air Base 2480.169216
Naha Airport / JASDF Naha Air Base 2501.407196

In [ ]:
!pip install folium matplotlib mapclassify

from mapclassify import classify
Requirement already satisfied: folium in /usr/local/lib/python3.10/dist-packages (0.18.0)
Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (3.8.0)
Collecting mapclassify
  Downloading mapclassify-2.8.1-py3-none-any.whl.metadata (2.8 kB)
Requirement already satisfied: branca>=0.6.0 in /usr/local/lib/python3.10/dist-packages (from folium) (0.8.0)
Requirement already satisfied: jinja2>=2.9 in /usr/local/lib/python3.10/dist-packages (from folium) (3.1.4)
Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from folium) (1.26.4)
Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from folium) (2.32.3)
Requirement already satisfied: xyzservices in /usr/local/lib/python3.10/dist-packages (from folium) (2024.9.0)
Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.3.1)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (4.55.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.4.7)
Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (24.2)
Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (11.0.0)
Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (3.2.0)
Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (2.8.2)
Requirement already satisfied: networkx>=2.7 in /usr/local/lib/python3.10/dist-packages (from mapclassify) (3.4.2)
Requirement already satisfied: pandas!=1.5.0,>=1.4 in /usr/local/lib/python3.10/dist-packages (from mapclassify) (2.2.2)
Requirement already satisfied: scikit-learn>=1.0 in /usr/local/lib/python3.10/dist-packages (from mapclassify) (1.5.2)
Requirement already satisfied: scipy>=1.8 in /usr/local/lib/python3.10/dist-packages (from mapclassify) (1.13.1)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2>=2.9->folium) (3.0.2)
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas!=1.5.0,>=1.4->mapclassify) (2024.2)
Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.10/dist-packages (from pandas!=1.5.0,>=1.4->mapclassify) (2024.2)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
Requirement already satisfied: joblib>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=1.0->mapclassify) (1.4.2)
Requirement already satisfied: threadpoolctl>=3.1.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=1.0->mapclassify) (3.5.0)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->folium) (3.4.0)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->folium) (3.10)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->folium) (2.2.3)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->folium) (2024.8.30)
Downloading mapclassify-2.8.1-py3-none-any.whl (59 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 59.1/59.1 kB 2.8 MB/s eta 0:00:00
Installing collected packages: mapclassify
Successfully installed mapclassify-2.8.1
In [ ]:
base=largeAirports.explore(color='red',marker_kwds=dict(radius=10))
rivers[rivers.name.str.contains('TOHORO')].explore(m=base)
Out[ ]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [ ]:
#Ahora, vamos a centrarnos en las varias polilíneas que se refieren a un mismo río.
rivers[~rivers.name.isna()]
#Esto sucede porque cada línea forma una parte de un mismo sistema de ríos
Out[ ]:
f_code hyc lit name soc geometry
18 BH140 8 1 ANBO G. JPN LINESTRING (640341.959 494783.487, 640533.83 4...
23 BH140 8 1 KIMOTSUKI G. JPN LINESTRING (668114.864 615221.236, 666979.228 ...
25 BH140 8 1 KIMOTSUKI G. JPN LINESTRING (663537.594 615098.715, 661949.242 ...
26 BH140 8 1 MANOSE G. JPN LINESTRING (608555.528 613449.112, 608760.762 ...
27 BH140 8 1 MANOSE G. JPN LINESTRING (599901.222 620731.187, 600053.928 ...
... ... ... ... ... ... ...
4779 BH140 8 1 SARUFUTSU G. JPN LINESTRING (1427986.228 2301873.03, 1428031.85...
4786 BH140 8 1 SARUFUTSU G. JPN LINESTRING (1428701.64 2302851.219, 1429871.74...
4787 BH140 8 1 KOETOI G. JPN LINESTRING (1392376.889 2306214.343, 1393127.7...
4790 BH140 8 2 ONUMA JPN LINESTRING (1391412.015 2307235.768, 1391198.8...
4791 BH140 8 1 KOETOI G. JPN LINESTRING (1390505.97 2308978.942, 1390526.22...

1273 rows × 6 columns

In [ ]:
#Disolvamos los que pertenecen a un mismo sistema de ríos en una multilínea:
name=rivers.dissolve(by='name')
name
Out[ ]:
geometry f_code hyc lit soc
name
ABASHIRI KO MULTILINESTRING ((1619681.843 2195475.388, 161... BH140 8 2 JPN
ABE K. MULTILINESTRING ((1319703.289 1085929.375, 131... BH140 8 1 JPN
ABIRA G. LINESTRING (1456120.306 2002642.709, 1456709.7... BH140 8 1 JPN
ABU G. MULTILINESTRING ((696983.361 949047.64, 696948... BH140 8 1 JPN
ABUKUBA G. LINESTRING (1480155.147 1474218.862, 1480677.6... BH140 8 2 JPN
... ... ... ... ... ...
YUFUTSU G. LINESTRING (1428896.05 2007936.152, 1429050.3 ... BH140 8 1 JPN
YUMESAKI G. LINESTRING (976663.994 1025342.223, 975670.044... BH140 8 1 JPN
YURA G. MULTILINESTRING ((1040815.46 1085564.646, 1041... BH140 8 1 JPN
YURAPPU G. LINESTRING (1333769.036 1936014.009, 1333840.8... BH140 8 1 JPN
YUSUHARA G. LINESTRING (837832.473 833123.256, 838060.352 ... BH140 8 1 JPN

378 rows × 5 columns

In [ ]:
name.reset_index(drop=False,inplace=True)
name
Out[ ]:
name geometry f_code hyc lit soc
0 ABASHIRI KO MULTILINESTRING ((1619681.843 2195475.388, 161... BH140 8 2 JPN
1 ABE K. MULTILINESTRING ((1319703.289 1085929.375, 131... BH140 8 1 JPN
2 ABIRA G. LINESTRING (1456120.306 2002642.709, 1456709.7... BH140 8 1 JPN
3 ABU G. MULTILINESTRING ((696983.361 949047.64, 696948... BH140 8 1 JPN
4 ABUKUBA G. LINESTRING (1480155.147 1474218.862, 1480677.6... BH140 8 2 JPN
... ... ... ... ... ... ...
373 YUFUTSU G. LINESTRING (1428896.05 2007936.152, 1429050.3 ... BH140 8 1 JPN
374 YUMESAKI G. LINESTRING (976663.994 1025342.223, 975670.044... BH140 8 1 JPN
375 YURA G. MULTILINESTRING ((1040815.46 1085564.646, 1041... BH140 8 1 JPN
376 YURAPPU G. LINESTRING (1333769.036 1936014.009, 1333840.8... BH140 8 1 JPN
377 YUSUHARA G. LINESTRING (837832.473 833123.256, 838060.352 ... BH140 8 1 JPN

378 rows × 6 columns

In [ ]:
#Dado que en conjunto representan un sistema de ríos, se cambiará el nombre
name = name.rename(columns={'name': 'system'})
name
Out[ ]:
system geometry f_code hyc lit soc
0 ABASHIRI KO MULTILINESTRING ((1619681.843 2195475.388, 161... BH140 8 2 JPN
1 ABE K. MULTILINESTRING ((1319703.289 1085929.375, 131... BH140 8 1 JPN
2 ABIRA G. LINESTRING (1456120.306 2002642.709, 1456709.7... BH140 8 1 JPN
3 ABU G. MULTILINESTRING ((696983.361 949047.64, 696948... BH140 8 1 JPN
4 ABUKUBA G. LINESTRING (1480155.147 1474218.862, 1480677.6... BH140 8 2 JPN
... ... ... ... ... ... ...
373 YUFUTSU G. LINESTRING (1428896.05 2007936.152, 1429050.3 ... BH140 8 1 JPN
374 YUMESAKI G. LINESTRING (976663.994 1025342.223, 975670.044... BH140 8 1 JPN
375 YURA G. MULTILINESTRING ((1040815.46 1085564.646, 1041... BH140 8 1 JPN
376 YURAPPU G. LINESTRING (1333769.036 1936014.009, 1333840.8... BH140 8 1 JPN
377 YUSUHARA G. LINESTRING (837832.473 833123.256, 838060.352 ... BH140 8 1 JPN

378 rows × 6 columns

In [ ]:
#Otra matriz de distancias
distanceMatrixKM_sys_air=name.set_index('system').geometry.apply\
(lambda g: largeAirports.set_index('name').geometry.distance(g)/1000).\
sort_index(axis=0).sort_index(axis=1)

distanceMatrixKM_sys_air
Out[ ]:
name Chubu Centrair International Airport Fukuoka Airport Kadena Air Base Kagoshima Airport Kansai International Airport Naha Airport / JASDF Naha Air Base Narita International Airport New Chitose Airport Osaka International Airport Sendai Airport Tokyo Haneda International Airport Yokota Air Base
system
ABASHIRI KO 1217.283686 1677.182631 2480.229305 1816.532215 1333.154795 2501.435605 990.433074 244.551514 1289.520308 718.752026 1031.244435 1024.571465
ABE K. 144.983225 752.549728 1404.981922 802.095667 296.530054 1426.017681 197.428037 898.156506 272.126096 398.721079 137.065171 106.327763
ABIRA G. 986.107165 1428.597944 2236.137954 1570.402794 1094.514545 2257.309983 794.416866 9.515936 1050.567774 519.663076 827.203575 813.805987
ABU G. 471.180435 126.047354 958.063141 290.426400 324.278622 978.679111 813.569313 1285.798056 343.992731 934.729471 754.546203 718.136116
ABUKUBA G. 516.816790 1074.143714 1806.245385 1170.230932 655.682134 1827.452077 265.057045 538.129229 615.991719 5.783667 301.731212 295.988611
... ... ... ... ... ... ... ... ... ... ... ... ...
YUFUTSU G. 983.783901 1416.725077 2227.784381 1560.932181 1089.287158 2248.933151 791.841251 9.936390 1045.255579 517.076335 824.669051 811.313853
YUMESAKI G. 196.752862 412.020326 1149.985786 496.844342 67.899759 1171.219711 533.547005 1066.577779 71.687692 665.207353 475.023172 437.853553
YURA G. 132.514027 473.345685 1223.352566 568.673177 93.659352 1244.586170 459.421105 995.587692 52.156038 591.629144 401.353183 363.562105
YURAPPU G. 891.326203 1302.876721 2119.461831 1450.901618 988.902091 2140.558714 738.826818 131.905972 944.783387 472.158064 763.389468 743.754020
YUSUHARA G. 393.011867 232.048127 913.742596 263.398089 240.533547 934.955208 740.857837 1306.427070 275.752309 902.421690 679.818443 649.522678

378 rows × 12 columns

In [ ]:
#Se obtienen todas las distancias mínimas
mins=distanceMatrixKM_sys_air.idxmin(axis="columns") # same as axis=1
mins
Out[ ]:
0
system
ABASHIRI KO New Chitose Airport
ABE K. Yokota Air Base
ABIRA G. New Chitose Airport
ABU G. Fukuoka Airport
ABUKUBA G. Sendai Airport
... ...
YUFUTSU G. New Chitose Airport
YUMESAKI G. Kansai International Airport
YURA G. Osaka International Airport
YURAPPU G. New Chitose Airport
YUSUHARA G. Fukuoka Airport

378 rows × 1 columns


In [ ]:
#Se elige uno de ellos
mins.iloc[1]
Out[ ]:
'Yokota Air Base'
In [ ]:
base=name.explore()
# the closest
largeAirports[largeAirports.name.isin(mins)].explore(m=base,color='red',marker_kwds=dict(radius=10))
# NOT the closest
largeAirports[~largeAirports.name.isin(mins)].explore(m=base,color='blue',marker_kwds=dict(radius=5))
Out[ ]:
Make this Notebook Trusted to load map: File -> Trust Notebook

Ejercicio 3¶

Distancia usando buffers¶

  1. Cree un CASCO para un conjunto de mapas de líneas.

  2. Calcule la matriz de distancia entre los CASCO y un mapa de puntos.

  3. Dibuje los CASCO y los puntos. Muestre los puntos más cercanos y más lejanos al CASCO.

In [ ]:
name.convex_hull
Out[ ]:
0
0 POLYGON ((1619821.192 2190479.177, 1619655.376...
1 POLYGON ((1319703.289 1085929.375, 1319291.634...
2 POLYGON ((1456120.306 2002642.709, 1459254.619...
3 POLYGON ((699425.217 946543.319, 698896.171 94...
4 POLYGON ((1489593.48 1473915.864, 1480155.147 ...
... ...
373 POLYGON ((1456782.408 2000091.379, 1456330.922...
374 POLYGON ((976663.994 1025342.223, 975670.044 1...
375 POLYGON ((1040815.46 1085564.646, 1014604.814 ...
376 POLYGON ((1342483.195 1933283.827, 1339369.542...
377 POLYGON ((837832.473 833123.256, 831300.539 85...

378 rows × 1 columns


In [ ]:
name.convex_hull.plot()
Out[ ]:
<Axes: >
No description has been provided for this image
In [ ]:
name_hulls=name.convex_hull.to_frame()
name_hulls['name']=name['system'].tolist()
name_hulls.rename(columns={0:'geometry'},inplace=True)
name_hulls=name_hulls.set_geometry('geometry')
name_hulls.crs="EPSG:6684"
name_hulls
Out[ ]:
geometry name
0 POLYGON ((1619821.192 2190479.177, 1619655.376... ABASHIRI KO
1 POLYGON ((1319703.289 1085929.375, 1319291.634... ABE K.
2 POLYGON ((1456120.306 2002642.709, 1459254.619... ABIRA G.
3 POLYGON ((699425.217 946543.319, 698896.171 94... ABU G.
4 POLYGON ((1489593.48 1473915.864, 1480155.147 ... ABUKUBA G.
... ... ...
373 POLYGON ((1456782.408 2000091.379, 1456330.922... YUFUTSU G.
374 POLYGON ((976663.994 1025342.223, 975670.044 1... YUMESAKI G.
375 POLYGON ((1040815.46 1085564.646, 1014604.814 ... YURA G.
376 POLYGON ((1342483.195 1933283.827, 1339369.542... YURAPPU G.
377 POLYGON ((837832.473 833123.256, 831300.539 85... YUSUHARA G.

378 rows × 2 columns

In [ ]:
#La matriz de distancias
distanceMatrixKM_sysHull_air=name_hulls.set_index('name').geometry.apply\
(lambda g: largeAirports.set_index('name').geometry.distance(g)/1000).\
sort_index(axis=0).sort_index(axis=1)

distanceMatrixKM_sysHull_air
Out[ ]:
name Chubu Centrair International Airport Fukuoka Airport Kadena Air Base Kagoshima Airport Kansai International Airport Naha Airport / JASDF Naha Air Base Narita International Airport New Chitose Airport Osaka International Airport Sendai Airport Tokyo Haneda International Airport Yokota Air Base
name
ABASHIRI KO 1217.283686 1677.182631 2480.229305 1816.532215 1333.154795 2501.435605 990.433074 244.551514 1289.520308 718.752026 1031.244435 1024.571465
ABE K. 144.983225 752.549728 1404.981922 802.095667 296.530054 1426.017681 197.428037 898.156506 272.069251 398.721079 137.065171 106.327763
ABIRA G. 986.107165 1428.597944 2236.137954 1570.402794 1094.514545 2257.309983 794.416866 8.779286 1050.567774 519.663076 827.203575 813.805987
ABU G. 471.180435 126.047354 958.063141 290.426400 324.278622 978.679111 813.569313 1285.798056 343.992731 934.729471 754.546203 718.136116
ABUKUBA G. 516.816790 1074.143714 1806.245385 1170.230932 655.682134 1827.452077 265.057045 538.129229 615.991719 5.783667 301.731212 295.988611
... ... ... ... ... ... ... ... ... ... ... ... ...
YUFUTSU G. 983.680813 1416.725077 2227.784381 1560.932181 1089.287158 2248.933151 791.841251 9.527550 1045.255579 517.076335 824.669051 811.313853
YUMESAKI G. 196.752862 412.020326 1149.985786 496.844342 67.899759 1171.219711 533.547005 1066.577779 71.677993 665.207353 475.023172 437.853553
YURA G. 132.514027 473.345685 1223.352566 568.673177 93.659352 1244.586170 459.421105 995.587692 52.156038 591.629144 401.353183 363.562105
YURAPPU G. 891.326203 1302.876721 2119.461831 1450.901618 988.902091 2140.558714 738.826818 131.905972 944.783387 472.158064 763.389468 743.754020
YUSUHARA G. 393.011867 232.047915 913.742596 263.398089 240.533547 934.955208 740.857837 1306.427070 275.752309 902.421690 679.818443 649.522678

378 rows × 12 columns

In [ ]:
mins=distanceMatrixKM_sysHull_air.idxmin(axis="columns")
mins
Out[ ]:
0
name
ABASHIRI KO New Chitose Airport
ABE K. Yokota Air Base
ABIRA G. New Chitose Airport
ABU G. Fukuoka Airport
ABUKUBA G. Sendai Airport
... ...
YUFUTSU G. New Chitose Airport
YUMESAKI G. Kansai International Airport
YURA G. Osaka International Airport
YURAPPU G. New Chitose Airport
YUSUHARA G. Fukuoka Airport

378 rows × 1 columns


In [ ]:
base=name_hulls.explore()
largeAirports[largeAirports.name.isin(mins)].explore(m=base,color='red',marker_kwds=dict(radius=10))
largeAirports[~largeAirports.name.isin(mins)].explore(m=base,color='blue',marker_kwds=dict(radius=5))
Out[ ]:
Make this Notebook Trusted to load map: File -> Trust Notebook

Ejercicio 4¶

Distancia usando buffers¶

  1. Seleccione un mapa de líneas y uno de puntos.

  2. Obtenga el área de influencia de las líneas y seleccione una distancia.

  3. Conserve los puntos que se encuentran dentro del área de influencia

In [ ]:
#Se usará el sistema disuelto
distanceMatrixKM_sys_air
Out[ ]:
name Chubu Centrair International Airport Fukuoka Airport Kadena Air Base Kagoshima Airport Kansai International Airport Naha Airport / JASDF Naha Air Base Narita International Airport New Chitose Airport Osaka International Airport Sendai Airport Tokyo Haneda International Airport Yokota Air Base
system
ABASHIRI KO 1217.283686 1677.182631 2480.229305 1816.532215 1333.154795 2501.435605 990.433074 244.551514 1289.520308 718.752026 1031.244435 1024.571465
ABE K. 144.983225 752.549728 1404.981922 802.095667 296.530054 1426.017681 197.428037 898.156506 272.126096 398.721079 137.065171 106.327763
ABIRA G. 986.107165 1428.597944 2236.137954 1570.402794 1094.514545 2257.309983 794.416866 9.515936 1050.567774 519.663076 827.203575 813.805987
ABU G. 471.180435 126.047354 958.063141 290.426400 324.278622 978.679111 813.569313 1285.798056 343.992731 934.729471 754.546203 718.136116
ABUKUBA G. 516.816790 1074.143714 1806.245385 1170.230932 655.682134 1827.452077 265.057045 538.129229 615.991719 5.783667 301.731212 295.988611
... ... ... ... ... ... ... ... ... ... ... ... ...
YUFUTSU G. 983.783901 1416.725077 2227.784381 1560.932181 1089.287158 2248.933151 791.841251 9.936390 1045.255579 517.076335 824.669051 811.313853
YUMESAKI G. 196.752862 412.020326 1149.985786 496.844342 67.899759 1171.219711 533.547005 1066.577779 71.687692 665.207353 475.023172 437.853553
YURA G. 132.514027 473.345685 1223.352566 568.673177 93.659352 1244.586170 459.421105 995.587692 52.156038 591.629144 401.353183 363.562105
YURAPPU G. 891.326203 1302.876721 2119.461831 1450.901618 988.902091 2140.558714 738.826818 131.905972 944.783387 472.158064 763.389468 743.754020
YUSUHARA G. 393.011867 232.048127 913.742596 263.398089 240.533547 934.955208 740.857837 1306.427070 275.752309 902.421690 679.818443 649.522678

378 rows × 12 columns

In [ ]:
# Escogiendo un rio , valor mínimo
distanceMatrixKM_sys_air.loc['ARA K.'].min()
Out[ ]:
11.965887088929707
In [ ]:
minMts=distanceMatrixKM_sys_air.loc['ARA K.'].min()*50 # km

# Buffer del rio
rivers[rivers.name=='ARA K.'].buffer(distance = minMts)
Out[ ]:
0
1864 POLYGON ((1439908.441 1195134.728, 1439919.852...
1869 POLYGON ((1439498.477 1195512.157, 1439539.668...
1882 POLYGON ((1436961.577 1197909.268, 1438127.45 ...
1918 POLYGON ((1429488.988 1200446.419, 1429819.741...
2036 POLYGON ((1410331.337 1211455.729, 1410387.052...
2118 POLYGON ((1402890.236 1221662.765, 1403827.204...
2159 POLYGON ((1401719.121 1222743.958, 1401690.837...
2235 POLYGON ((1386093.467 1235060.938, 1386509.885...
2236 POLYGON ((1363369.923 1224412.622, 1363390.125...
2237 POLYGON ((1383360.896 1234649.125, 1383404.249...
3350 POLYGON ((1363213.181 1457258.844, 1363146.259...

In [ ]:
# Visualización
bufferAroundARA=rivers[rivers.name=='ARA K.'].buffer(distance = minMts)
bufferAsBase=bufferAroundARA.explore(color='red')
rivers[rivers.name=='ARA K.'].explore(m=bufferAsBase,color='blue',style_kwds={'weight':1})
Out[ ]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [ ]:
#  Importando los aeropuertos pequeños
small_airports=airports[airports['kind']=='small_airport']

# plotting
rivers[rivers.name=='ARA K.'].explore(m=bufferAsBase,color='blue',style_kwds={'weight':0.5})
small_airports.explore(m=bufferAsBase,color='black')
Out[ ]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [ ]:
# Mantener los aeropuertos dentro del buffer
riversWithinBuffer=small_airports.clip(mask=bufferAroundARA)
riversWithinBuffer
Out[ ]:
name kind latitude_deg longitude_deg elevation_ft iso_region municipality geometry
403 Kumagaya Flying Club Osato Airfield small_airport 36.112460 139.413100 NaN JP-11 Kumagaya POINT (1392627.42 1233045.482)
3029 Kumagaya Trike Club Airfield small_airport 36.121580 139.400290 86.0 JP-11 Kumagaya POINT (1391295.745 1233878.823)
320 Honda Airport small_airport 35.976330 139.524125 NaN JP-11 Kawajima POINT (1405226.731 1219424.722)
395 Mint Airstrip small_airport 36.002160 139.498000 NaN JP-11 Kawajima POINT (1402372.569 1221933.565)
397 Yuhikoku Kawajima Airstrip small_airport 36.011448 139.500289 NaN JP-11 Kawajima POINT (1402411.305 1223009.37)
394 Tokyo Flying Club Airstrip small_airport 36.035460 139.500880 NaN JP-11 Konosu POINT (1402025.772 1225711.421)
401 Air Wing Motor Paraglider School Osato Landing... small_airport 36.087990 139.431460 NaN JP-11 Kumagaya POINT (1394744.455 1230573.005)
361 Iwabuchi Watergate Auxiliary Airfield small_airport 35.782031 139.738133 NaN JP-13 Kita, Tokyo POINT (1428344.169 1200829.351)
In [ ]:
# ploteo de aeropuertos dentro del buffer
bufferAsBase=bufferAroundARA.explore(color='red')
rivers[rivers.name=='ARA K.'].explore(m=bufferAsBase,color='blue',style_kwds={'weight':1})
riversWithinBuffer.explore(m=bufferAsBase,color='black')
Out[ ]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [ ]:
# Mínimo de todos los mínimos por fila
distanceMatrixKM_sys_air.min(axis=1).min()
Out[ ]:
0.7952852405805073
In [ ]:
# usando el valor anterior
minMinMts_5=5*distanceMatrixKM_sys_air.min(axis=1).min()*500

allMinBuffer=rivers.buffer(distance = minMinMts_5).explore(color='red')
rivers.explore(m=allMinBuffer,color='blue',style_kwds={'weight':0.75})
Out[ ]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [ ]:
# Buffer de todos los ríos
riversAll_buf=rivers.buffer(distance = minMinMts_5)
riversAll_buf
Out[ ]:
0
18 POLYGON ((633554.485 497789.938, 633481.88 497...
23 POLYGON ((665455.602 616562.567, 666384.034 61...
25 POLYGON ((661946.206 613112.93, 661751.342 613...
26 POLYGON ((608598.171 611463.601, 608476.211 61...
27 POLYGON ((599133.659 618899.535, 599066.47 618...
... ...
4779 POLYGON ((1430008.535 2301665.045, 1430020.051...
4786 POLYGON ((1429256.295 2305122.691, 1429444.566...
4787 POLYGON ((1392111.274 2304170.183, 1391609.272...
4790 POLYGON ((1389739.838 2308307.381, 1389774.041...
4791 POLYGON ((1389427.709 2306943.308, 1389401.863...

1273 rows × 1 columns


In [ ]:
# información
allRiversWithinBuffs=small_airports.clip(riversAll_buf)
allRiversWithinBuffs
Out[ ]:
name kind latitude_deg longitude_deg elevation_ft iso_region municipality geometry
1303 Taragi Auxiliary Airfield small_airport 32.276669 130.915607 NaN JP-43 Taragi POINT (652126.25 716707.08)
467 Shirakawa Gliderport small_airport 32.776029 130.630532 NaN JP-43 Kumamoto POINT (621720.608 770527.433)
3508 Ozuki Airfield small_airport 34.045300 131.052002 13.0 JP-35 Shimonoseki POINT (651703.34 914283.914)
469 Hofu Gliderport small_airport 34.039370 131.540310 NaN JP-35 Hofu POINT (696972.88 916865.687)
855 Ryu's Skypark small_airport 34.638053 136.545568 NaN JP-24 Tsu POINT (1153412.048 1029972.088)
... ... ... ... ... ... ... ... ...
1653 Yausubetsu Airfield small_airport 43.287200 144.997800 108.0 JP-01 Betsukai POINT (1705595.702 2136981.909)
466 Betsukai Flight Park small_airport 43.474840 144.783090 407.0 JP-01 Betsukai POINT (1682695.261 2153291.301)
460 Kitami Agricultural Airfield / Skyport Kitami small_airport 43.779980 143.730640 609.0 JP-01 Kitami POINT (1589025.95 2165571.973)
1660 Otaki Airfield small_airport 42.665802 141.101933 1621.0 JP-01 Date POINT (1403367.264 1992761.454)
463 Takikawa Sky Park small_airport 43.549349 141.894103 75.0 JP-01 Takikawa POINT (1446781.404 2105034.171)

65 rows × 8 columns

In [ ]:
# Ploteo
base=riversAll_buf.plot(color='yellow')
allRiversWithinBuffs.plot(ax=base, color='green', markersize=1)
Out[ ]:
<Axes: >
No description has been provided for this image
In [ ]:
# Visualización
base=riversAll_buf.explore(color='yellow')
allRiversWithinBuffs.explore(m=base, color='green')
Out[ ]:
Make this Notebook Trusted to load map: File -> Trust Notebook

Ejercicio 5¶

  1. Obtenga un mapa de polígonos de la unidad administrativa más baja posible.

  2. Obtenga una tabla de variables para esas unidades. Al menos 3 variables numéricas.

  3. Preprocese ambas tablas y prepárelas para la fusión.

  4. Realice la fusión, haciendo los cambios necesarios para conservar la mayor cantidad de columnas.

In [ ]:
# Se carga el mapa de polígonos (la unidad administrativa más baja disponible)
units_admin = gpd.read_file(
    "https://github.com/brayan-lliuya-2024/TAREA_4-Grupo5/raw/refs/heads/main/maps/mapsjapan_ej3.gpkg"
)
In [ ]:
# Se inspecciona las primeras filas
units_admin.head()
Out[ ]:
name geometry
0 Japan MULTILINESTRING ((1635374.655 2197865.102, 163...
In [ ]:
# Graficamos para verificar
units_admin.plot()
Out[ ]:
<Axes: >
No description has been provided for this image
In [ ]:
import random
import pandas as pd
import geopandas as gpd

# Se crea una tabla de variables asociada a las unidades administrativas
# Las unidades administrativas tienen una columna "name" para poder asociar las variables.
variables_table = pd.DataFrame({
    "name": units_admin["name"],  # Tomar el nombre de la unidad administrativa
    "population": [random.randint(1000, 5000) for _ in range(len(units_admin))],  # Ejemplo: población
    "area_km2": [random.uniform(10, 100) for _ in range(len(units_admin))],      # Ejemplo: área
    "density": [random.uniform(50, 300) for _ in range(len(units_admin))]        # Ejemplo: densidad
})

print(variables_table.head()) #Se observan las variables
    name  population   area_km2     density
0  Japan        1553  96.130479  239.785251
In [ ]:
# Preprocesar las tablas
# Se asegura que ambas tablas tienen el mismo tipo en las columnas clave
units_admin["name"] = units_admin["name"].astype(str)
variables_table["name"] = variables_table["name"].astype(str)
In [ ]:
# Se fusionan las tablas
fusion_data = units_admin.merge(variables_table, on="name", how="left")
In [ ]:
# Se verifica el resultado de la fusión
fusion_data.head()
Out[ ]:
name geometry population area_km2 density
0 Japan MULTILINESTRING ((1635374.655 2197865.102, 163... 1553 96.130479 239.785251
In [ ]:
# Finalmente, se guarda el resultado como un nuevo archivo GeoPackage
fusion_data.to_file("unidad_admin_fusionada.gpkg", driver="GPKG")
In [ ]:
# Se visualiza el resultado
fusion_data.plot(column="population", legend=True)
Out[ ]:
<Axes: >
No description has been provided for this image

Ejercicio 7¶

  1. Calcule el coeficiente de Moran para una de las tres variables numéricas.

  2. Haga un diagrama de dispersión para cada variable.

In [ ]:
# Instalar las librerías necesarias
!pip install libpysal esda splot

import geopandas as gpd
from libpysal.weights import KNN
from esda.moran import Moran
from splot.esda import moran_scatterplot
import matplotlib.pyplot as plt
In [ ]:
# Cargar el shapefile o geopackage con los datos espaciales
datadisMap = gpd.read_file(
    "https://github.com/brayan-lliuya-2024/TAREA_4-Grupo5/raw/refs/heads/main/maps/mapsjapan_ej2.gpkg"
)
In [ ]:
datadisMap.head()
Out[ ]:
name kind latitude_deg longitude_deg elevation_ft iso_region municipality geometry
0 New Ishigaki Airport medium_airport 24.396389 124.245000 102.0 JP-47 Ishigaki POINT (24849.797 -177601.73)
1 JMSDF Chichijima Airfield seaplane_base 27.090231 142.192032 NaN JP-13 Ogasawara POINT (1821915.408 255473.101)
2 Urayasu Heliport heliport 35.623658 139.894506 16.0 JP-12 Urayasu POINT (1445601.261 1185419.126)
3 Yagishiri Emergency Heliport heliport 44.439286 141.400487 NaN JP-01 Haboro POINT (1385302.891 2195212.393)
4 Yokohama Heliport heliport 35.342222 139.656389 4.0 JP-14 Yokohama POINT (1428917.497 1150234.837)
In [ ]:
datadisMap.dtypes
Out[ ]:
0
name object
kind object
latitude_deg float64
longitude_deg float64
elevation_ft float64
iso_region object
municipality object
geometry geometry

In [ ]:
# Para observar la cantidad de variables numericas
numeric_columns = datadisMap.select_dtypes(include=['float64', 'int64']).columns
print(f"Variables numéricas en el dataset: {list(numeric_columns)}")
Variables numéricas en el dataset: ['latitude_deg', 'longitude_deg', 'elevation_ft']
In [ ]:
# Verificar las nuevas variables
print(datadisMap[['latitude_deg', 'longitude_deg', 'elevation_ft']].head())
   latitude_deg  longitude_deg  elevation_ft
0     24.396389     124.245000         102.0
1     27.090231     142.192032           NaN
2     35.623658     139.894506          16.0
3     44.439286     141.400487           NaN
4     35.342222     139.656389           4.0
In [ ]:
# Seleccionar una de las variables numéricas
variable = "longitude_deg"
In [ ]:
# Crear matriz de vecinos usando KNN (K=8)
k = 8
w_knn8 = KNN.from_dataframe(datadisMap, k=k)
/usr/local/lib/python3.10/dist-packages/libpysal/weights/distance.py:153: UserWarning: The weights matrix is not fully connected: 
 There are 2 disconnected components.
  W.__init__(self, neighbors, id_order=ids, **kwargs)
In [ ]:
# Transformar los pesos para que sumen 1
w_knn8.transform = "R"
In [ ]:
from libpysal.weights.distance import KNN

# Calcular el coeficiente de Moran para la variable seleccionada
moran = Moran(datadisMap[variable], w_knn8)
print(f"Coeficiente de Moran: {moran.I}")
print(f"Valor p-simulado: {moran.p_sim}")
Coeficiente de Moran: 0.9916672561677757
Valor p-simulado: 0.001
In [ ]:
# Crear el diagrama de dispersión de Moran
fig, ax = moran_scatterplot(moran, aspect_equal=True)
ax.set_xlabel(f"{variable}_std")
ax.set_ylabel(f"SpatialLag_{variable}_std")
ax.set_title(f"Diagrama de Dispersión de Moran - {variable}")
plt.show()
No description has been provided for this image